home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4562 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.7 KB

  1. Path: newsfeed.gsfc.nasa.gov!usenet
  2. From: Dirk Broer <Dirk.Broer@gsfc.nasa.gov>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Interpreting whitespace w. cin
  5. Date: Fri, 26 Jan 1996 20:00:09 -0800
  6. Organization: NASA Goddard Space Flight Center -- Greenbelt, Maryland USA
  7. Message-ID: <3109A349.642D@gsfc.nasa.gov>
  8. References: <4emb7p$u0@guitar.hal.com>
  9. NNTP-Posting-Host: control.gsfc.nasa.gov
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b5 (Win16; I)
  14.  
  15. Blair Martin wrote:
  16. > I want to write a program that uses new and delete to dynamically
  17. > allocate exactly enough free memory to hold string input data.
  18. > That is, instead of allocating a fixed array of
  19. > characters from the stack, receive one character at a time and
  20. > continually allocate, copy, and release space so that the physical
  21. > and logical lengths of the buffer area are always the same.
  22. > When the newline character is encountered, print the string
  23. > and release the space. Then prompt for the next string. When
  24. > EOF is entered, terminate the program.
  25. > The problem is with a loop like
  26. >     while (!(cin >> input).eof())
  27. >     {
  28. >             ...
  29. >     }
  30. > there is no way to know when a newline is encountered since
  31. > all whitespace is ignored.
  32. > Is there another way to do this?
  33. > blair
  34.  
  35. Try this - not pretty as far as memory usage goes but it might work for 
  36. you:
  37.  
  38. char    buffer[256];
  39.  
  40. while( !(cin.getline( buffer, 256 ).eof()) )
  41. {
  42.     strstream    temp;
  43.     
  44.     temp << buffer;
  45.     while (!(temp >> input).eof())
  46.     {
  47.              ...
  48.     }
  49. }
  50.  
  51. Ok, not pretty - but it basically parses cin line by line and then parses 
  52. the individual lines into tokens.
  53.  
  54. Dirk
  55.